package com.huawei.fakedouyin.util;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.huawei.fakedouyin.R;

/**
 * 全屏PopupWindow 监听键盘高度
 */
public class KeyboardHeightProvider extends PopupWindow {
    /** The tag for logging purposes */
    private final static String TAG = "sample_KeyboardHeightProvider";
    /** The keyboard height observer */
    private KeyboardHeightObserver observer;
    /** The cached landscape height of the keyboard */
    private int keyboardLandscapeHeight;
    /** The cached portrait height of the keyboard */
    private int keyboardPortraitHeight;
    /** The view that is used to calculate the keyboard height */
    private View popupView;
    /** The parent view */
    private View parentView;
    /** The root activity that uses this KeyboardHeightProvider */
    private Activity activity;
    /**
     * Construct a new KeyboardHeightProvider
     *
     * @param activity The parent activity
     */
    public KeyboardHeightProvider(Activity activity) {
        super(activity);
        this.activity = activity;
        LayoutInflater inflator = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        this.popupView = inflator.inflate(R.layout.keyboard_popup_window, null, false);
        setContentView(popupView);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE );
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        parentView = activity.getWindow().getDecorView();
        setWidth(1);
//        setOutsideTouchable(false);
//        setClippingEnabled(false);
//        setFocusable(false);

        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        popupView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (popupView != null) {
                    handleOnGlobalLayout();
                }
            }
        });
    }
    /**
     * Start the KeyboardHeightProvider, this must be called after the onResume of the Activity.
     * PopupWindows are not allowed to be registered before the onResume has finished
     * of the Activity.
     */
    public void start() {
        if (!isShowing() && parentView.getWindowToken() != null) {
            LogUtil.Companion.getInstance().logDebug("show start");
            setBackgroundDrawable(new ColorDrawable(0));
            showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0);
        }else {
            LogUtil.Companion.getInstance().logDebug("show false");
        }
    }
    /**
     * Close the keyboard height provider,
     * this provider will not be used anymore.
     */
    public void close() {
        dismiss();
    }

    /**
     * Get the screen orientation
     *
     * @return the screen orientation
     */
    private int getScreenOrientation() {
        return activity.getResources().getConfiguration().orientation;
    }
    /**
     * Popup window itself is as big as the window of the Activity.
     * The keyboard can then be calculated by extracting the popup view bottom
     * from the activity window height.
     */
    private void handleOnGlobalLayout() {
        Point screenSize = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
        Rect rect = new Rect();
        popupView.getWindowVisibleDisplayFrame(rect);
// REMIND, you may like to change this using the fullscreen size of the phone
// and also using the status bar and navigation bar heights of the phone to calculate
// the keyboard height. But this worked fine on a Nexus.
        int orientation = getScreenOrientation();
        int keyboardHeight = ScreenUtilKt.screenHeight() - rect.bottom;
        if (keyboardHeight == 0) {
            notifyKeyboardHeightChanged(0, orientation);
        }
        else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            this.keyboardPortraitHeight = keyboardHeight;
            notifyKeyboardHeightChanged(keyboardPortraitHeight, orientation);
        }
        else {
            this.keyboardLandscapeHeight = keyboardHeight;
            notifyKeyboardHeightChanged(keyboardLandscapeHeight, orientation);
        }
    }
    private void notifyKeyboardHeightChanged(int height, int orientation) {
        Log.d("KeyboardHeightProvider","notifyKeyboardHeightChanged "+orientation + "高度:" + height);
        LiveDataSet.keyBoardHeight.postValue(height);
    }
}
